<?php
/** 
 * Basic router controller
 * 
 * @author Alton Crossley <crossleyframework@nogahidebootstrap.com>
 * @package Crossley Framework
 *  
 * @license BSD http://opensource.org/licenses/bsd-license.php
 * @version $Id:$
 * 
 */
/**
 * simple export controler
 *
 */
class X_Controller_Router implements X_Controller_Interface
{
    /**
     * tell authentication to be fired
     *
     * @var bool
     */
    public $bAuthenticate = false;
    /**
     * tell authentication to require one of the following rolls
     * comma seperated
     *
     * @var string
     */
    public $sRolls = null;
    public $sAutErrorHandler = "X_Controller_Noauth";
    /**
     * tell caching to be used
     *
     * @var bool
     */
    public $bCache = false;
    /**
     * set to false to override caching globally
     *
     * @var unknown_type
     */
    public static $bGlobalAllowCache = true;
    /**
     * allow html to hold the place of a contoler action
     *
     * @var bool
     */
    private static $_bHtmlFileContent = false;
    /**
     * allow use of the Broker Generator
     *
     * @var unknown_type
     */
    private static $_bGenerate = false;
    /**
     * prefix for handler classes
     *
     * @var string
     */
    protected $_sHandlerPrefix = "Handle_";
    /**
     * the default response type
     *
     * @var string constant
     */
    protected $_cResponse = X_Broker_Response::ENCODE_HTML;
    /**
     * @return boolean
     */
    public static function getGenerate()
    {
        return self::$_bGenerate;
    }
    /**
     * @param boolean $_bGenerate
     */
    public static function setGenerate($_bGenerate = true)
    {
        self::$_bGenerate = $_bGenerate;
    }
    /**
     * @return bool
     */
    public function getHtmlFileContent()
    {
        return self::$_bHtmlFileContent;
    }
    /**
     * @param bool $_bHtmlFileContent
     */
    public function setHtmlFileContent($bHtmlFileContent = true)
    {
        self::$_bHtmlFileContent = $bHtmlFileContent;
    }
    function __construct($bAllowHtmlContent = false)
    {
        $this->setHtmlFileContent($bAllowHtmlContent);
    }
    /**
     * handle input
     *
     * @param Array Filtered SAE (Simple Array Event) Array
     * @return variant
     */
    public function respond(X_Broker_Event_Interface $oEvent)
    {
        $bAuthorized = false;
        if ($this->bAuthenticate)
        {
            $oUser = $oEvent->getUser();
            if ($oUser->confirm())
            {
                if ($this->sRolls != null)
                {
                    $aRolls = explode(",", $this->sRolls);
                    foreach ($aRolls as $sRollName)
                    {
                        if ($oUser->isPermitted($sRollName))
                        {
                            $bAuthorized = true;
                        }
                    }
                    // @todo fire an event and handeler if not authorized
                }
            }
            else
            {
                $oUser->identify();
            }
        }
        if (X_Controller_Router::$bGlobalAllowCache && $this->bCache && X_Cache::zc()->test($oEvent->getUid()))
        {
            return X_Cache::zc()->load($oEvent->getUid());
        }
        // assign a default value
        $xResponse = null;
        // get the name of the handler for this data
        $sHandler = $oEvent->getStep();
        // give a step if this is the index - use start to
        // avoid including index.php or default.php
        if (empty($sHandler)) $sHandler = 'index';
        // then get the event handler name
        $sCurrentEventHandler = $this->_sHandlerPrefix . X_String::toCapWords($sHandler);
        // if a controller interface, run it that way
        if (class_exists($sCurrentEventHandler, true) && in_array('X_Controller_Handler_Interface', class_implements($sCurrentEventHandler)))
        {
            // create handler instance
            $oEventHandler = new $sCurrentEventHandler();
            if ($oEventHandler->isSecure == false || $oUser->isPermitted($sCurrentEventHandler))
            {
                $xResponse = $oEventHandler->handle($oEvent);
            }
        }
        elseif (self::$_bHtmlFileContent)
        {
            // build foe file name
            $sHtmlFileName = $sCurrentEventHandler;
            $sHtmlFileName = X_Broker::$sContentPath . X_String::toCapWords($sHtmlFileName) . '.html';
            if (file_exists($sHtmlFileName))
            {
                $this->_cResponse = X_Broker_Response::ENCODE_HTML;
                $xResponse = file_get_contents($sHtmlFileName);
            }
            else
            {
                /**
                 * @todo enable handler generation when HTML content is enabled
                 */
                if (self::$_bGenerate)
                {
                    // add the class name to the data
                    $oEvent->setData(array('gen_class_name' => $sCurrentEventHandler , 'gen_file_name' => $sHtmlFileName), true);
                    // reset the path to handle everything in order
                    $oEvent->resetPath();
                    $oDevController = new X_Controller_Handler_Generator();
                    $oDevController->bHtmlContent = self::$_bHtmlFileContent;
                    $xResponse = $oDevController->handle($oEvent);
                }
                else
                {
                    throw new X_Controller_Exception('unable to route event or import content');
                }
            }
        }
        else
        {
            if (self::$_bGenerate)
            {
                // add the class name to the data
                self::$oEvent->setData(array('gen_class_name' => $sClassName), true);
                // reset the path to handle everything in order
                self::$oEvent->resetPath();
                $oDevController = new X_Controller_Generator();
                $oDevController->bHtmlContent = self::$_bHtmlFileContent;
                $xResponse = $oDevController->respond($oEvent);
            }
            else
            {
                throw new X_Controller_Exception('unable to route event');
            }
        }
        if (X_Controller_Router::$bGlobalAllowCache && $this->bCache)
        {
            X_Cache::zc()->save($xResponse, $oEvent->getUid());
        }
        return $xResponse;
    }
}
